#! /bin/sh

# From: https://wiki.gentoo.org/wiki/Custom_Initramfs
cmdline() {
    local value
    value=" $(cat /proc/cmdline) "
    value="${value##* $1=}"
    value="${value%% *}"
    [ "$value" != "" ] && echo "$value"
}

# Get the slot kernel parameter
slot_name="$(cmdline slot)"
if [ -z "${slot_name}" ]; then
    echo "Is the slot= kernel parameter missing?"
    exit 1
fi

# Systems with lots of CPUs need nr_cpus > 1 in the capture kernel
avail_cpus="$(/usr/bin/nproc --all)"
if [ ${avail_cpus} -ge 64 ]; then
    nr_cpus="16"
else
    nr_cpus="1"
fi

# Need the boot file system so we can load a kernel
/bin/mount /boot || exit 1

# Load a kernel (prod or debug) for use on panic
case "`/bin/uname -r`" in
    *prod*)
        kernel=/boot/${slot_name}-bzImage-esos.prod
        initramfs=/boot/${slot_name}-initramfs.cpio.gz
        /usr/sbin/kexec -p ${kernel} --initrd=${initramfs} \
            --append="single irqpoll nr_cpus=${nr_cpus} reset_devices"
    ;;
    *debug*)
        kernel=/boot/${slot_name}-bzImage-esos.debug
        initramfs=/boot/${slot_name}-initramfs.cpio.gz
        /usr/sbin/kexec -p ${kernel} --initrd=${initramfs} \
            --append="single irqpoll nr_cpus=${nr_cpus} reset_devices"
    ;;
esac

# Done
/bin/umount /boot || exit 1

